package com.codedrunks.components.containers
{
	import mx.modules.ModuleLoader;	
	import mx.events.ResizeEvent;	

	import flexlib.containers.WindowShade;	
	
	import mx.events.FlexEvent;	
	
	import flash.events.Event;	
	import flash.events.TimerEvent;	
	import flash.utils.Timer;
	
	import com.arc90.flexlib.containers.CollapsiblePanel;	
	
	import mx.core.EdgeMetrics;	

	import flexlib.events.WindowShadeEvent;	
	import mx.core.Container;
	import mx.containers.VBox;	
	import flash.display.DisplayObject;
	
	
	public class CollapsibleWindow extends CollapsiblePanel
	{
		private var vbox:VBox;
		private var timer:Timer;
		public var selectedIndexes:Array = [0];
		public var useWindowShade:Boolean = true;
		private var childsList:Array = new Array();
		
		public function CollapsibleWindow()
		{
			super();
			
			vbox = new VBox();
			vbox.horizontalScrollPolicy = 'off';
			vbox.verticalScrollPolicy = 'off';
			this.horizontalScrollPolicy = 'off';
			this.verticalScrollPolicy = 'off';
			
			this.addEventListener(ResizeEvent.RESIZE, handleResize);
			
			this.setStyle('titleStyleName', 'headerStyle');
			this.setStyle('borderThicknessBottom',3);
   			this.setStyle('borderThicknessLeft', 3);
 		 	this.setStyle('borderThicknessRight', 3);
   			this.setStyle('borderThicknessTop', 3);
   			this.setStyle('headerHeight', 20);
			this.setStyle('borderThickness', 0);
   			this.setStyle('borderStyle', 'solid');
   			this.setStyle('borderAlpha', 0.56);
		   	this.setStyle('roundedBottomCorners', true);
		   	this.setStyle('cornerRadius', 4);
		   	this.setStyle('backgroundAlpha', 0.3);
		    this.setStyle('backgroundColor', '#999999');
		   	this.setStyle('highlightAlphas', [0.48, 0.17]);
		   	this.setStyle('headerColors', ['#999999', '#000000']);
		   	this.setStyle('dropShadowEnabled', true);
		   	
		   	this.addEventListener('test', handleTest);
		   	
		}
		
		/*
		 * overriding the initialize method for layouting
		 */
		override public function initialize():void
		{
			super.initialize();
			
			this.horizontalScrollPolicy = "off";
			this.verticalScrollPolicy = "off";
			this.setStyle('paddingTop', 0);
			this.setStyle('paddingLeft', 0);
			this.setStyle('paddingRight', 0);
			this.setStyle('paddingBottom', 0);
			this.setStyle('padding', 0);
			
			this.showCloseButton = true;
			this.maximizable = false;
			
			vbox.percentWidth = this.width;
			vbox.percentHeight = this.height;
			vbox.setStyle('verticalGap', 0);
			
		}
	
		
		/*
		 * overriding addChild
		 */
		override public function addChild(child:DisplayObject):DisplayObject
		{
			childsList.push(child);
			if(useWindowShade)
			{
				var windowShade:WindowShade = new WindowShade();
				windowShade.percentWidth = 100;
				windowShade.height = child.height + windowShade.borderMetrics.top;
				windowShade.label = Container(child).label;
				windowShade.setStyle("paddingTop", 0);
				windowShade.addEventListener(WindowShadeEvent.OPEN_END, handleResizeEndEvent);
				windowShade.addEventListener(WindowShadeEvent.CLOSE_END, handleResizeEndEvent);
				windowShade.addEventListener(WindowShadeEvent.OPEN_BEGIN, handleResizeBeginEvent);
				windowShade.addEventListener(WindowShadeEvent.CLOSE_BEGIN, handleResizeBeginEvent);
				windowShade.addEventListener(FlexEvent.CREATION_COMPLETE, handleWindowCreationComplete);
				vbox.addChild(windowShade);
				windowShade.addChild(child);
			}
			else
			{
				vbox.addChild(child);
			}
			handleResizeEndEvent(null);
			if(vbox.stage == null) super.addChild(vbox);
			return child;
		}
		
		/*
		 * comment
		 */
		private function handleWindowCreationComplete(event:FlexEvent):void
		{
			for(var i:int = 0; i<selectedIndexes.length; i++)
			{
				if(event.target == vbox.getChildAt(selectedIndexes[i]))
				{
					WindowShade(event.target).opened = true;
					return;
				}
				else
				{
					WindowShade(event.target).opened = false;
				}
			}
		}
		
		/*
		 * method to handle the open end event
		 */
		private function handleResizeEndEvent(event:WindowShadeEvent):void
		{
			if(timer != null) 
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, resizeWindow);
			}
			resizeWindow();
		}
		
		
		/*
		 * method to handle the open end event
		 */
		private function handleResizeBeginEvent(event:WindowShadeEvent):void
		{
			if(timer != null)
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, resizeWindow);
			}
			timer = new Timer(10);
			timer.addEventListener(TimerEvent.TIMER, resizeWindow);
			timer.start();
		}
		
		/*
		 * methode to resize window
		 */
		private function resizeWindow(event:TimerEvent = null):void
		{
			var h:Number = this.borderMetrics.top + this.borderMetrics.bottom;
			for(var i:int = 0; i<vbox.getChildren().length; i++)
			{
				h += vbox.getChildAt(i).height;	
			}
			this.height = h;
		}
		
		/*
		 * handle resize event
		 */
		private function handleResize(event:ResizeEvent):void
		{
			if(event.oldWidth != 0 && event.oldHeight != 0)
			{
				for(var i:int=0; i< childsList.length; i++)
				{
					if(childsList[i] is ModuleLoader)
					{
						var mLoader:ModuleLoader = childsList[i] as ModuleLoader;  
						mLoader.child.width = event.oldWidth;
						if(!useWindowShade) mLoader.child.height = event.oldHeight;
					}
				}
			}
		}
		
		/*
		 * test
		 */
		private function handleTest(event:Event):void
		{
			trace("debug --> testing... ", event.type);			
		}
	}
}